import cover2Api from '../api/cover2'
import coverApi from '../api/cover'
import mgtApi from '@/api/mgt.js'
export default {
  getResList: function({ commit }) {
    return $http.get(`${window.RPConfig.mgt}/role/getResList`).then(response => {
      var resList = response.data
      let list = []
      for (let i = 0; i < resList.length; i++) {
        list.push(resList[i].resourceCode)
      }
      commit('setResList', list)
      return response.data
    })
  },
  getUserBu({ commit }) {
    cover2Api.getUserBu().then(res => {
      const userBu = res.data
      commit('setUserBu', userBu)
      return userBu
    })
  },
  getUserBuByDataEle({ commit }) {
    cover2Api.getUserBu({ areaCode: 'cdi' }).then(res => {
      const userBu = res.data
      commit('setDataEleUserBu', userBu)
      return userBu
    })
  },
  // 从值集中获取填报周期、状态
  getBuList({ commit }) {
    coverApi.getCodeList('BU').then(res => {
      const quarterList = JSON.parse(res.data.records.filter(item => item.vsiKey === 'BU_TIME_LIST')[0].vsiDescription)
      const stateList = JSON.parse(res.data.records.filter(item => item.vsiKey === 'BU_STATUS_LIST')[0].vsiDescription)
      const writeablePeriod = quarterList.filter(item => item.writeable === true).map(item => item.value)[0]
      commit('setWriteablePeriod', writeablePeriod)
      commit('setQuarterList', quarterList)
      commit('setStateList', stateList)
      return { quarterList, stateList }
    })
  },
  // 获取业务单元
  getBuUnit({ commit }) {
    cover2Api.getBuList().then(res => {
      const buList = res.data.data
      commit('setBuList', buList)
    })
  },
  // 从值集中获取填报周期、状态
  async getMenuList({ commit }) {
    try {
      const res = await coverApi.getCodeList('MENU');
      const menuList = res.data.records.reduce((acc, item) => {
        if (item.enabled === 1) {
          acc.push({
            value: item.vsiKey,
            label: item.vsiValue,
            children: JSON.parse(item.vsiDescription),
            obj: item
          });
        }
        return acc
      }, [])
  
      commit('setMenuList', menuList)
    } catch (error) {
      console.error('Error fetching menu list:', error)
    }
  },
  // 从值集中获取填报周期、状态
  async getOrgList({ commit }) {
    try {
      // 获取根节点
      const root = await mgtApi.getAuthorizedOrgs('')
      if (!root || root.length === 0) {
        console.error('No root organization found.')
        return
      }
  
      let orgData = []
      // 获取一级业务单元
      const org = await mgtApi.getAuthorizedOrgs(root[0].orgPath)
      orgData = org
  
      // 获取集团总部（假设是 orgData[0] 的子节点）
      if (orgData.length > 0) {
        const item = await mgtApi.getAuthorizedOrgs(orgData[0].orgPath)
        orgData[0].children = item
        commit('setOrgList', orgData)
      } else {
        console.error('No first-level organization found.')
      }
    } catch (error) {
      console.error('Error fetching organization data:', error)
    }
  },

  async getLoginData({ commit }) {
    const res = await mgtApi.getLoginData()
    const loginData = res.userBasicVo
    commit('setLoginData', loginData)
    return loginData
  }
}
